package event;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class PageConfig extends Config{
	private static final String EVENT_CONFIG_FILE = "EventConfig.xml";
	private static final String EVENT_ENTRY = "EventEntry";
	private static final String NAME = "Name";
	private static final String LINK_ATTRIBUTE = "LinkAttribute";
	private static final String PARAMETER = "Parameter";
	private static final String LINK_NAME = "LinkName";
	private static final String ATTR_NAME = "AttrName";
	private static final String TYPE = "Type";
	private static final String LINK_TYPE = "LinkType";
	private static final String LINK = "Link";
	private static final String MAX = "Max";
	private static final String MIN = "Min";
	private static final String RATIO = "Ratio";
	private Map<String, EventNode> eventNodeMap = new HashMap<String,EventNode>(); 
	private List<EventNode> eventNodeList = new LinkedList<EventNode>();
	public PageConfig()
	{
		super(EVENT_CONFIG_FILE);
		loadConfig();
	}
	protected void loadConfig()
	{
		readFile();
		NodeList eventList = config.getElementsByTagName(EVENT_ENTRY);
		for(int i = 0; i < eventList.getLength(); i++)
		{
			// load the information of the basic attributes
			Element eventEntry = (Element)(eventList.item(i));
			EventNode eventNode = new EventNode();
			Node eventName = eventEntry.getElementsByTagName(NAME).item(0);
			eventNode.name = eventName.getFirstChild().getNodeValue();
			NodeList linkNodeList = eventEntry.getElementsByTagName(LINK);
			List<Link> linkList = new LinkedList<Link>();
			Map<String, Link> linkMap = new HashMap<String, Link>();
			// load the information of the links
			for(int j = 0; j < linkNodeList.getLength(); j++)
			{
				Link link = new Link();
				Element linkElem = (Element)(linkNodeList.item(j));
				Node linkName = linkElem.getElementsByTagName(LINK_NAME).item(0);
				link.linkName = linkName.getFirstChild().getNodeValue();
				Node linkType = linkElem.getElementsByTagName(LINK_TYPE).item(0);
				link.linkType = linkType.getFirstChild().getNodeValue();
				Node paramNode = linkElem.getElementsByTagName(PARAMETER).item(0);
				List<String> params = new LinkedList<String>();
				String param = paramNode.getFirstChild().getNodeValue();
				params.add(param);
				link.params = params;
				Node maxAttr = linkElem.getElementsByTagName(MAX).item(0);
				int max = Integer.parseInt(maxAttr.getFirstChild().getNodeValue());
				link.max = max;
				Node minAttr = linkElem.getElementsByTagName(MIN).item(0);
				int min = Integer.parseInt(minAttr.getFirstChild().getNodeValue());
				link.min = min; 
				Node ratio  = linkElem.getElementsByTagName(RATIO).item(0);
				link.ratio = Double.parseDouble(ratio.getFirstChild().getNodeValue());
				NodeList linkAttrList = linkElem.getElementsByTagName(LINK_ATTRIBUTE);
				List<Attribute> attrs = new LinkedList<Attribute>();
				//load the attributes for the links
				for(int k = 0; k < linkAttrList.getLength(); k++)
				{
					Attribute attr = new Attribute();
					Element linkAttrElem = (Element)(linkAttrList.item(k));
					Node attrName = linkAttrElem.getElementsByTagName(ATTR_NAME).item(0);
					attr.name = attrName.getFirstChild().getNodeValue();
					Node attrType = linkAttrElem.getElementsByTagName(TYPE).item(0);
					attr.type = attrType.getFirstChild().getNodeValue(); 
					List<String> linkAttrParams = new LinkedList<String>();
					NodeList linkAttrParamList = linkAttrElem.getElementsByTagName(PARAMETER);
					for(int l = 0; l < linkAttrParamList.getLength(); l++)
					{
						Node linkAttrParamNode = linkAttrParamList.item(l);
						String linkAttrParam = linkAttrParamNode.getFirstChild().getNodeValue();
						linkAttrParams.add(linkAttrParam);
					}
					attr.params = linkAttrParams;
					attrs.add(attr);
				}
				link.attrs = attrs;
				linkList.add(link);
				linkMap.put(link.linkName, link);
			}
			eventNode.linkList = linkList;
			eventNode.linkMap = linkMap;
			eventNodeMap.put(eventNode.name,eventNode);
			eventNodeList.add(eventNode);
		}
	}
	public EventNode getEventNode(String name)
	{
		return eventNodeMap.get(name);
	}
	public Map<String, EventNode> getEventNodeMap()
	{
		return eventNodeMap;
	}
	public List<EventNode> getEventNodeList()
	{
		return eventNodeList;
	}
}
